//
//  AppDelegate.swift
//  MacMenu
//
//  Created by LL on 2023/9/7.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

#if targetEnvironment(macCatalyst)
extension AppDelegate {
    
    override func buildMenu(with builder: UIMenuBuilder) {
        //  1. 删除默认菜单
        //  删除文件菜单
        builder.remove(menu: .file)
        //  删除编辑菜单
        builder.remove(menu: .edit)
        //  删除应用菜单下的服务菜单
        builder.remove(menu: .services)
        
        //  2. 往默认菜单中插入菜单
        
        let keyMenu = UIMenu(title: "键盘按键", options: .destructive, children: [
            UIKeyCommand(title: "1",
                         action: #selector(command1Action),
                         input: "1",
                         modifierFlags: .command),
            UIKeyCommand(title: "P",
                         action: #selector(commandAndShiftPAction),
                         input: "P",
                         modifierFlags: [.command, .shift])
        ])
        builder.insertChild(keyMenu, atStartOfMenu: .window)
        
        //  3. 新增菜单
        
        let newWindowMenu = UIMenu(title: "新窗口", options: .destructive, children: [
            UICommand(title: "缩放", action: #selector(scaleAction)),
            UIKeyCommand(title: "关闭", action: #selector(closeAction), input: "W", modifierFlags: .command)
        ])
        builder.insertSibling(newWindowMenu, afterMenu: .application)
    }
    
    @objc private func command1Action() {
        print("command + 1")
    }
    
    @objc private func commandAndShiftPAction() {
        print("command + shift + P")
    }
    
    @objc private func scaleAction() {
        print("缩放")
    }
    
    @objc private func closeAction() {
        print("关闭")
    }
}
#endif
